package com.tekj.cxqcb.view.Module.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.library.flowlayout.FlowLayoutManager;
import com.squareup.picasso.Picasso;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.SelectExpandDetailBean;
import com.tekj.cxqcb.view.Module.adapter.RelatedChannelsAdapter;
import com.tekj.cxqcb.view.Module.adapter.Screen4Adapter;
import com.tekj.cxqcb.view.Module.bean.ScreenBean;
import com.tekj.cxqcb.view.Module.decoration.SpacesItemDecoration;

import org.angmarch.views.NiceSpinner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import common.widget.CircleTransform;

/**
 * 关联渠道Activity
 */
public class RelatedChannelsActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    //    @BindView(R.id.tv_identity)
//    TextView tvIdentity;
//    @BindView(R.id.tv_region)
//    TextView tvRegion;
//    @BindView(R.id.tv_time)
//    TextView tvTime;
    @BindView(R.id.tv_Superior_name)
    TextView tvSuperiorName;
    @BindView(R.id.tv_Superior_identity)
    TextView tvSuperiorIdentity;
    @BindView(R.id.tv_Superior_region)
    TextView tvSuperiorRegion;
    @BindView(R.id.rv_subordinate)
    RecyclerView rvSubordinate;
    @BindView(R.id.nice_spinner)
    NiceSpinner niceSpinner;
    @BindView(R.id.ll_superior)
    LinearLayout llSuperior;
    @BindView(R.id.img_head)
    ImageView imgHead;
    private RelatedChannelsAdapter adapter;
    private BusinessDao businessDao;
    private int page = 1;
    private String queryType, phone, querySort = "1";

    private Screen4Adapter screenAdapter, screenAdapter2;

    @Override
    public void init() {
        tvTitle.setText("关联渠道");
        businessDao = new BusinessDao(this, this);
        if (MainBActivity.User.getRole().equals("4")) {
            tvR.setVisibility(View.GONE);
        }
        initAdapter();
        GetData();

    }

    @Override
    public int setLayout() {
        return R.layout.activity_related_channels;
    }

    boolean isfirst = true;

    @Override
    public void initLoadStart() {
        if (isfirst) {
            loading.show();
        }
    }

    @Override
    public void initLoadEnd() {
        loading.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {

        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 关联渠道:
                SelectExpandDetailBean detailBean = (SelectExpandDetailBean) bindingViewBean.getBean();
                if (detailBean.getCode().equals("0000000")) {
                    if (isfirst) {
                        SetViewData(detailBean.getData());
                        isfirst = false;
                    }
                    if (page == 1) {
                        adapter.setNewData(detailBean.getData().getList());
                    } else {
                        adapter.addData(detailBean.getData().getList());
                    }
                    adapter.loadMoreComplete();
                    if (detailBean.getData().getList().size() < 10) {
                        adapter.loadMoreEnd();
                    }

                }

                break;
        }

    }


    @OnClick({R.id.ll_back, R.id.tv_title, R.id.tv_call, R.id.ll_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_right:
                startActivity(new Intent(activity, AllOrdersActivity.class));
                break;
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_title:
                showPop(screenAdapter, screenAdapter2);
                break;
            case R.id.tv_call:
                Tools.CallPhone(activity, phone);
                break;
        }
    }

    void GetData() {
        businessDao.SelectExpandDetail(MainBActivity.User.getUserId(), page, queryType, querySort);
    }

    /**
     * 设置初始数据
     *
     * @param dataBean
     */
    private void SetViewData(SelectExpandDetailBean.DataBean dataBean) {
        String identity = "", superiorIdentity = "";
        if (dataBean.getRole().equals("3")) {
            identity = "信审员";
        } else if (dataBean.getRole().equals("4")) {
            identity = "城市合伙人";

        } else if (dataBean.getRole().equals("5") || dataBean.getRole().equals("6")) {
            identity = "运管中心";
        } else if (dataBean.getRole().equals("7") || dataBean.getRole().equals("8")) {
            identity = "省分公司";
            llSuperior.setVisibility(View.GONE);
        }

        if (dataBean.getParentRole().equals("4")) {
            superiorIdentity = "城市合伙人";
            setTextColorBG(activity, tvSuperiorIdentity, 2);
        } else if (dataBean.getParentRole().equals("5") || dataBean.getParentRole().equals("6")) {
            superiorIdentity = "运管中心";
            setTextColorBG(activity, tvSuperiorIdentity, 1);
        } else if (dataBean.getParentRole().equals("7") || dataBean.getParentRole().equals("8")) {
            superiorIdentity = "省分公司";
            setTextColorBG(activity, tvSuperiorIdentity, 3);
        }
//        tvIdentity.setText("用户身份：" + identity);
//        tvRegion.setText("代理区域：" + dataBean.getAddress());
//        tvTime.setText("加入时间：" + dataBean.getCreateTime());

        tvSuperiorIdentity.setText(superiorIdentity);
        tvSuperiorName.setText(dataBean.getParentName());
        tvSuperiorRegion.setText(dataBean.getParentAddress());

        List<String> dataList = new ArrayList<>();
        dataList.add("全部");
        dataList.add("城市合伙人");
        if (dataBean.getRole().equals("7") || dataBean.getRole().equals("8"))
            dataList.add("运管中心");
        niceSpinner.attachDataSource(dataList);
        niceSpinner.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        queryType = "";
                        break;
                    case 1:
                        queryType = "4";
                        break;
                    case 2:
                        queryType = "5";
                        break;
                }
                page = 1;
                GetData();
            }
        });
        Picasso.get()
                .load(dataBean.getHead().equals("") ? "1" : dataBean.getHead())
                .transform(new CircleTransform())
                .error(R.mipmap.icon_head)
                .placeholder(R.mipmap.icon_head)
                .into(imgHead);

    }

    void initAdapter() {

        adapter = new RelatedChannelsAdapter(null);
        rvSubordinate.setLayoutManager(new LinearLayoutManager(activity));
        rvSubordinate.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                GetData();
            }
        }, rvSubordinate);
        rvSubordinate.addItemDecoration(new SpacesItemDecoration(15));
        adapter.setEmptyView(R.layout.rv_empty);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(activity, SubordinateDetailsActivity.class)
                        .putExtra("id", RelatedChannelsActivity.this.adapter.getData().get(position))

                );
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_phone:
                        Tools.CallPhone(activity, RelatedChannelsActivity.this.adapter.getData().get(position).getPhone());
                        break;
                }
            }
        });

        List<ScreenBean> list = new ArrayList<>();
        list.add(new ScreenBean("全部渠道", "", ""));
        list.add(new ScreenBean("仅看运管中心", "5", ""));
        list.add(new ScreenBean("仅看城市合伙人", "4", ""));
        if (MainBActivity.User.getSystemPhone() == 1) {
            list.add(new ScreenBean("仅看省分公司", "7", ""));
        }

        List<ScreenBean> list2 = new ArrayList<>();
        list2.add(new ScreenBean("首字母排序", "1", ""));
        list2.add(new ScreenBean("加盟时间排序", "2", ""));
        list2.add(new ScreenBean("订单数排序", "3", ""));

        screenAdapter = new Screen4Adapter(list);
        screenAdapter2 = new Screen4Adapter(list2);

        screenAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                screenAdapter.setCheckItem(position);
                tvTitle.setText(screenAdapter.getData().get(screenAdapter.getCheckItemPosition()).getName());
                queryType = screenAdapter.getData().get(screenAdapter.getCheckItemPosition()).getId();
                page = 1;
                GetData();
            }
        });
        screenAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                screenAdapter2.setCheckItem(position);
                querySort = screenAdapter2.getData().get(screenAdapter2.getCheckItemPosition()).getId();
                page = 1;
                GetData();
            }
        });
    }

    public static void setTextColorBG(Context activity, TextView view, int type) {
        int BgId = 0, TextColor = 0;

        if (type == 1) {
            BgId = R.drawable.bg_identity_r;
            TextColor = R.color.color_FF0000;
        } else if (type == 2) {
            BgId = R.drawable.bg_identity_y;
            TextColor = R.color.color_ED700F;
        } else if (type == 3) {
            BgId = R.drawable.bg_identity_b;
            TextColor = R.color.colorPrimary;
        }
        Drawable drawable = activity.getResources().getDrawable(BgId);
        view.setBackground(drawable);
        view.setTextColor(activity.getResources().getColor(TextColor));
    }


    private PopupWindow popupWindow;
    private View popupView;
    private TranslateAnimation animation;

    private void showPop(RecyclerView.Adapter adapter, RecyclerView.Adapter adapter2) {
        popupView = View.inflate(activity, R.layout.pop_top_screen, null);
        popupWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);


        animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, -1, Animation.RELATIVE_TO_PARENT, 0);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setDuration(200);


        RecyclerView rvScreen = popupView.findViewById(R.id.rv_screen);
        rvScreen.setLayoutManager(new FlowLayoutManager());
        rvScreen.setHasFixedSize(true);
        rvScreen.setAdapter(adapter);
        rvScreen.addItemDecoration(new SpacesItemDecoration(10));

        RecyclerView rvScreen2 = popupView.findViewById(R.id.rv_screen2);
        rvScreen2.setLayoutManager(new FlowLayoutManager());
        rvScreen2.setHasFixedSize(true);
        rvScreen2.setAdapter(adapter2);
        rvScreen2.addItemDecoration(new SpacesItemDecoration(10));

        View view= popupView.findViewById(R.id.mask);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popupWindow.showAsDropDown(rlTitleBg);
        popupView.startAnimation(animation);

    }


}
